package com.katsu.myapplib.util;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

public abstract class UtilAsyView extends Handler {

    public UtilAsyView(){

        new Thread(){

            @Override
            public void run() {

                try {

                    Thread.sleep(1);

                } catch (InterruptedException e) {}

                Looper.prepare();

                Message m = obtainMessage();

                try {

                    m.what = 0;

                    m.obj = handler();

                } catch (Exception e) {

                    m.what = 1;

                }

                m.sendToTarget();

                Looper.loop();

            }

        }.start();

    }

    @Override
    public void handleMessage(Message m) {

        if (m.what == 0) {

            result((View) m.obj);

        } else {

            try {

                result(handler());

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

    protected abstract View handler();

    protected abstract void result(View view);

}
